home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 24 / Mac Magazin and MacEasy Magazine CD - Issue 24.iso / Online / PageSpinner 1.2b3 / JavaScript Examples / Dynamic Document Stationery < prev    next >
Text File  |  1996-08-02  |  4KB  |  174 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE>Dynamic Document</TITLE>
  4.  
  5. <SCRIPT LANGUAGE="JavaScript">
  6. <!-- Beginning of JavaScript --------
  7. /*
  8.     Dynamic Document
  9.     Written by Jerry Aman, Optima System, Aug 2, 1996.
  10.     Part of the PageSpinner distribution.
  11.  
  12.     We will not be held responsible for any unwanted 
  13.     effects due to the usage of this script or any derivative.  
  14.     No warrantees for usability for any specific application are 
  15.     given or implied.
  16.  
  17.     You are free to use and modify this script,
  18.     if the credits are given in the source code
  19. */
  20.  
  21. // getHourOfDay()
  22. // Out: Integer containing the hour 0-24
  23.  
  24. function getHourOfDay()
  25. {       var now = new Date();
  26.         return(now.getHours());
  27. }
  28.  
  29.  
  30. // getTime()
  31. // Out: Text containing the time in the format HH:MM
  32.  
  33. function getTime()
  34. {       
  35.     var now = new Date();
  36.     var minutes = now.getMinutes();
  37.     var divider = ":";
  38.  
  39.     if (minutes<10)
  40.         divider = ":0";
  41.  
  42.     return( now.getHours() + divider + minutes );
  43. }
  44.  
  45.  
  46. // isMacintoshBrowser()
  47. // Out: Boolean value, true if the browser is running under MacOS
  48.  
  49. function isMacintoshBrowser()
  50.     return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  51. }
  52.  
  53.  
  54. // sayHello ()
  55. // Inserts text dynamically in the document when it is called
  56.  
  57. function sayHello ()
  58. {    
  59.     if(getHourOfDay()<5 || getHourOfDay()>19)
  60.         document.write('<FONT COLOR="FFFF00"> a good night!</FONT>');
  61.     else 
  62.     {
  63.         if ( getHourOfDay() <11) 
  64.         {
  65.             document.write('<FONT COLOR="52A553"> a good morning!</FONT>');
  66.         } 
  67.         else 
  68.         { 
  69.             document.write('<FONT COLOR="ED363C"> a good day!</FONT>');
  70.         }
  71.     }
  72. }
  73.  
  74. // -- End of JavaScript code -------------- -->
  75. </SCRIPT>
  76.  
  77. </HEAD>
  78. <BODY BGCOLOR=FFFFFF TEXT=000000>
  79. <H1>JavaScript Dynamic Document</H1>
  80.  
  81. <B>This stationery page contains JavaScript that creates a dynamic page</B>
  82. <P>
  83. Please note that JavaScript is currently only available in Netscape Navigator 2.0 or higher. <BR>
  84. <FONT COLOR="931B15">Do not assume that all in your audience are using a JavaScript enabled browser.</FONT>
  85. <HR>
  86. Here is an example on how JavaScript can be used to provide a dynamic page that will change its contents depending upon the local time specified in the browser's system and the OS that is used by the reader.
  87. <P>
  88.  
  89. Welcome <B><SCRIPT>
  90. <!--
  91. document.write(navigator.userAgent)
  92. // -->
  93. </SCRIPT></B>!
  94. <P>
  95.  
  96. Nice to see that you use 
  97. <SCRIPT>
  98. <!--
  99. document.write(navigator.appName," ", navigator.appVersion)
  100. // -->
  101. </SCRIPT>.
  102. <P>
  103.  
  104. I see that the time is <B>
  105. <SCRIPT>
  106. <!--
  107. document.write(getTime())
  108. // -->
  109. </SCRIPT></B>, so I wish you 
  110. <SCRIPT>
  111. <!--
  112. sayHello()
  113. // -->
  114. </SCRIPT>
  115.  
  116.  
  117. <SCRIPT>
  118. <!--
  119.  
  120. if (isMacintoshBrowser())
  121.     document.write(
  122.     '<P><FONT COLOR="385FD1">Very, very nice to see that you are using MacOS!</FONT>'); 
  123. else
  124.     document.write(
  125.     '<P>Sad too see that you are not using a Mac to view this page.')
  126. // -->
  127. </SCRIPT>
  128.  
  129. <HR>
  130.  
  131. <P>
  132. <B>How to use:</B><BR>
  133. Several small scripts are embedded inside the text contents of this file. The scripts are executed - and inserts text - when the page are loaded.
  134. <P>
  135. Replace the contents inside the script with calls to your own test functions and add text you want to have displayed inside <FONT COLOR="FF3366"><CODE>document.write()</CODE></FONT> function inside the script. It is recommended to create functions  for complex scripts and place these in the HEAD section, see the <TT>sayHello</TT> function for an axample of this. 
  136.  
  137. <P>
  138. Edit this page or copy selected scripts to create your own dynamic page!
  139. <P>
  140. <HR>
  141. <P>
  142. <B>Example code for the first dynamic text above:</B>
  143. <XMP>Welcome <B><SCRIPT>
  144. <!--
  145. document.write(navigator.userAgent)
  146. // -->
  147. </SCRIPT></B>!</XMP>
  148.  
  149. You <I>may</I> also write the script without the <FONT COLOR="555555"><!--</FONT> comment (see below), but if you do this the actual script may show up in a browser that doesn't support JavaScript.
  150. <XMP>Welcome, <B><SCRIPT>document.write(navigator.userAgent);</SCRIPT></B>!
  151. </XMP>
  152.  
  153. <P>
  154. <HR>
  155. <P>
  156. <B>This function located in the HEAD section returns true if the browser is running on a Mac:</B><BR>
  157.  
  158. <XMP>function isMacintoshBrowser()
  159.   return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  160. }
  161. </XMP>
  162.  
  163. The function can be used inside a script like this:
  164. <XMP>if (isMacintoshBrowser())
  165.   document.write('Mac specific text...'); 
  166. else
  167.   document.write('Text for other platforms...')
  168. </XMP>
  169.  
  170. </BODY>
  171. </HTML>
  172.